/**
 * This file contains all the entry routes
 */

const Router = require('restify-router').Router;
const { InternalServerError, BadRequestError } = require('restify-errors');
const validate = require('restify-api-validation');
const Joi = require('joi');
const _ = require('lodash');

const routerInstance = new Router();
const stockService = require('../service/stock');
const passport = require('../auth');

const gg = require('../global');

//  test, remove if import scripts are ready.
//  add
routerInstance.post(
    '',
    validate(
        {
            body: {
                name: Joi.string().required(),
                code: Joi.string().required(),
                shortcut: Joi.string().required(),
            }
        }
    ),
    async (req, res, next) => {
        let {
            name,
            code,
            shortcut
        } = req.body;

        let record = await stockService.add(name, code, shortcut);

        //res.json(entry);
        gg.result(res, record);
    }
);

//  view
routerInstance.get(
    '/:id',
    async (req, res, next) => {

        const id = req.params.id
        
        let record = await stockService.get(id);

        //res.json(entry);
        gg.result(res, record);
    }
);

//  update
routerInstance.put(
    '/:id',
    async (req, res, next) => {
        let {
            summary
        } = req.body;

        const id = req.params.id
        
        let record = await stockService.update(id, summary);

        //res.json(entry);
        gg.result(res, record);
    }
);

module.exports = routerInstance;